﻿using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Text;
using MySql.Data.MySqlClient;

namespace ServerGps
{
    class DBService
    {
        private static DBService instance;
        //private const string connString = "Database=btsm;Data Source=127.0.0.1;User Id=root;Password=;CharSet=GBK;port=3306";
        //private const string connString = "datasource=localhost;username=root;password=;database=btsm;charset=gb2312;";
        //private const string connString = "datasource=168.160.192.228;username=zjxia;password=bblovebb;database=wwwdzxuecom;charset=gb2312;";
        //private const string connStrings[2] = new string[] { "datasource=60.2.237.19;username=zjxia;password=bblovebb;database=wwwdzxuecom;charset=gb2312;", "datasource=60.2.237.19;username=zjxia;password=bblovebb;database=wwwdzxuecom;charset=gb2312;" };
        //private const string connString = "datasource=60.2.237.19;username=zjxia;password=bblovebb;database=wwwdzxuecom;charset=gb2312;";
        private MySqlConnection[] conns;
        string[] connStrings = { "datasource=db.yishiedu.com;username=zjxia;password=bblovebb;database=wwwdzxuecom;charset=gb2312;", "datasource=db.yishiedu.com;username=zjxia;password=bblovebb;database=wwwyishieducom;charset=gb2312;" };    //
        private MySqlConnection conn;
        private DBService() {
            init();
        }
        void init() {
            conns = new MySqlConnection[2];
            conns[0] = new MySqlConnection(connStrings[0]);
            conns[0].Open();
            conns[1] = new MySqlConnection(connStrings[1]);
            conns[1].Open();
            conn = conns[0];
        }
        MySqlConnection reconnect(MySqlConnection conn) {
            for (int i = 0; i < 2; ++i) {
                if (conn == conns[i]) {
                    conn = new MySqlConnection(connStrings[i]);
                    conns[i] = conn;
                    conn.Open();
                    return conn;
                }
            }
            return null;
        }
        public static DBService getInstance()
        {
            if (instance == null) {
                instance = new DBService(); 
            }
            return instance;
        }
        public MySqlDataReader query(String sql)
        {
            MySqlCommand comm = new MySqlCommand(sql, conn);
            return comm.ExecuteReader();
        }
        public void execute(string sql) {
            try
            {
                MySqlCommand comm = new MySqlCommand(sql, conn);
                comm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                //conn = new MySqlConnection(connString);
                //conn.Open();
                conn = reconnect(conn);
                MySqlCommand comm = new MySqlCommand(sql, conn);
                comm.ExecuteNonQuery();
            }
        }
        public object executeScalar(string sql) {
            MySqlCommand comm = new MySqlCommand(sql, conn);
            return comm.ExecuteScalar();
        }
        public void useDB(string db) {
            if (db == "wwwyishieducom" || db == "wwwdzxuecom")
            {
                conn = conns[1];
                try
                {
                    conn.ChangeDatabase(db);
                }
                catch (Exception e)
                {
                    conn = reconnect(conn);
                    conn.ChangeDatabase(db);
                }
            }
            else
            {
                conn = conns[0];
                try
                {
                    conn.ChangeDatabase(db);
                }
                catch (Exception e)
                {
                    conn = reconnect(conn);
                    conn.ChangeDatabase(db);
                }
            }
        }
        public void connect() {
        }
        public static void Main1() {
            MySqlDataReader reader = getInstance().query("select cn_name from board limit 3");
            while (reader.Read()) {
                Console.Out.WriteLine(reader[0].ToString());
                string result = reader[0].ToString();
                result = "";
            }
            Console.In.Read();
        }
    }
}
